<?php


namespace app\api\controller\awards;

use app\common\controller\Api;
use think\Db;
use \app\admin\model\user\dyfz\awards\Info as InfoModel;


class Info extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取奖状列表
     *
     */
    public function list($award_type)
    {
        $list = InfoModel::where('user_id', $this->auth->id)
            ->where('award_type', $award_type)
            ->paginate();
        $this->success('获取成功', $list);
    }

    /**
     * 提交奖状信息
     */
    public function submit()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['user_id'] = $this->auth->id;
            if (isset($params["id"])) {
                unset($params['create_time']);
                unset($params['award_type_text']);
                unset($params['award_time_text']);
                unset($params['create_time_text']);
                unset($params['update_time_text']);
                unset($params['award_type']);
                $params['update_time'] = time();
                $record = InfoModel::where('id', $params["id"])->update($params);
            } else {
                $record = InfoModel::insert($params);
            }
            if ($record) {
                $this->success('提交成功');
            }
            $this->error('提交失败');
        }
        $this->error('非法请求');
    }

    /**
     * 获取奖状信息
     */
    public function info($id)
    {
        $awards = InfoModel::where('id', $id)
            ->find();
        $this->success('获取成功', $awards);
    }
}
